package com.library.management.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.library.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.enums.BusinessType;
import com.library.management.domain.LibraryBook;
import com.library.management.service.ILibraryBookService;
import com.library.common.utils.poi.ExcelUtil;
import com.library.common.core.page.TableDataInfo;

/**
 * 图书Controller
 *
 * @author library
 * @date 2023-03-26
 */
@RestController
@Anonymous
@RequestMapping("/management/book")
public class LibraryBookController extends BaseController {
    @Autowired
    private ILibraryBookService libraryBookService;

    /**
     * 借书
     */
    @Anonymous
    @Log(title = "借书", businessType = BusinessType.UPDATE)
    @GetMapping("/borrowBooks/{ids}")
    public AjaxResult borrowBook(@PathVariable String[] ids) {
        return toAjax(libraryBookService.borrowBooks(ids));
    }

    /**
     * 查询图书列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('management:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(LibraryBook libraryBook) {
        startPage();
        List<LibraryBook> list = libraryBookService.selectLibraryBookList(libraryBook);
        return getDataTable(list);
    }

    /**
     * 导出图书列表
     */
    @PreAuthorize("@ss.hasPermi('management:book:export')")
    @Log(title = "图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LibraryBook libraryBook) {
        List<LibraryBook> list = libraryBookService.selectLibraryBookList(libraryBook);
        ExcelUtil<LibraryBook> util = new ExcelUtil<LibraryBook>(LibraryBook.class);
        util.exportExcel(response, list, "图书数据");
    }

    /**
     * 获取图书详细信息
     */
    @PreAuthorize("@ss.hasPermi('management:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(libraryBookService.selectLibraryBookById(id));
    }

    /**
     * 新增图书
     */
    @PreAuthorize("@ss.hasPermi('management:book:add')")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LibraryBook libraryBook) {
        return toAjax(libraryBookService.insertLibraryBook(libraryBook));
    }

    /**
     * 修改图书
     */
    @PreAuthorize("@ss.hasPermi('management:book:edit')")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LibraryBook libraryBook) {
        return toAjax(libraryBookService.updateLibraryBook(libraryBook));
    }

    /**
     * 删除图书
     */
    @PreAuthorize("@ss.hasPermi('management:book:remove')")
    @Log(title = "图书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(libraryBookService.deleteLibraryBookByIds(ids));
    }
}
